const Graph = require("../template")

function testGraphBasic() {
  const graph = new Graph()

  console.log(
    "1. 基本添加顶点测试:",
    (() => {
      graph.addVertex('A')
      graph.addVertex('B')
      graph.addVertex('C')
      const vertices = graph.getVertices()
      return vertices.includes('A') &&
             vertices.includes('B') &&
             vertices.includes('C') &&
             vertices.length === 3
    })()
  )

  console.log(
    "2. 基本添加边测试:",
    (() => {
      graph.addEdge('A', 'B')
      graph.addEdge('B', 'C')
      graph.addEdge('A', 'C')
      return graph.hasEdge('A', 'B') === true &&
             graph.hasEdge('B', 'C') === true &&
             graph.hasEdge('A', 'C') === true &&
             graph.hasEdge('B', 'A') === false
    })()
  )

  console.log(
    "3. 获取邻接顶点测试:",
    (() => {
      const neighborsA = graph.getNeighbors('A')
      const neighborsB = graph.getNeighbors('B')
      const neighborsC = graph.getNeighbors('C')
      return neighborsA.includes('B') &&
             neighborsA.includes('C') &&
             neighborsB.includes('C') &&
             neighborsC.length === 0
    })()
  )

  console.log(
    "4. 删除边测试:",
    (() => {
      graph.removeEdge('A', 'B')
      return graph.hasEdge('A', 'B') === false &&
             graph.hasEdge('B', 'C') === true &&
             graph.hasEdge('A', 'C') === true
    })()
  )

  console.log(
    "5. 删除顶点测试:",
    (() => {
      graph.removeVertex('B')
      const vertices = graph.getVertices()
      return vertices.includes('A') === true &&
             vertices.includes('B') === false &&
             vertices.includes('C') === true &&
             graph.hasEdge('A', 'B') === false &&
             graph.hasEdge('B', 'C') === false
    })()
  )

  console.log(
    "6. 图的基本属性测试:",
    (() => {
      return graph.getVertexCount() === 2 &&
             graph.getEdgeCount() === 1 &&
             graph.isEmpty() === false
    })()
  )

  console.log(
    "7. this上下文图测试:",
    (() => {
      const graphObj = {
        multiplier: 2,
        processGraph: function(vertices, edges) {
          const graph = new Graph()
          vertices.forEach(v => graph.addVertex(v))
          edges.forEach(([from, to]) => graph.addEdge(from, to))
          return graph.getVertexCount() * this.multiplier
        }
      }
      return graphObj.processGraph(['A', 'B'], [['A', 'B']]) === 4 // 2*2=4
    })()
  )

  console.log(
    "8. 复杂对象this绑定图测试:",
    (() => {
      const graphConfigObj = {
        config: { threshold: 2 },
        processWithConfig: function(vertices, edges) {
          const graph = new Graph()
          vertices.forEach(v => graph.addVertex(v))
          edges.forEach(([from, to]) => graph.addEdge(from, to))
          return graph.getVertexCount() >= this.config.threshold
        }
      }
      return graphConfigObj.processWithConfig(['A', 'B', 'C'], [['A', 'B']]) === true
    })()
  )

  console.log(
    "9. 图一致性测试:",
    (() => {
      const consistencyGraph = new Graph()
      consistencyGraph.addVertex('A')
      consistencyGraph.addVertex('B')
      consistencyGraph.addEdge('A', 'B')
      const count1 = consistencyGraph.getEdgeCount()
      consistencyGraph.removeEdge('A', 'B')
      const count2 = consistencyGraph.getEdgeCount()
      return count1 === 1 && count2 === 0
    })()
  )

  console.log(
    "10. 图边界测试:",
    (() => {
      const boundaryGraph = new Graph()
      boundaryGraph.addVertex('A')
      return boundaryGraph.getVertexCount() === 1 &&
             boundaryGraph.getEdgeCount() === 0 &&
             boundaryGraph.isEmpty() === false
    })()
  )

  console.log(
    "11. 图性能测试:",
    (() => {
      const perfGraph = new Graph()
      const vertices = Array.from({ length: 100 }, (_, i) => `V${i}`)
      const start = performance.now()
      vertices.forEach(v => perfGraph.addVertex(v))
      for (let i = 0; i < 100; i += 2) {
        perfGraph.addEdge(`V${i}`, `V${i + 1}`)
      }
      const end = performance.now()
      return perfGraph.getVertexCount() === 100 && (end - start) < 1000
    })()
  )

  console.log(
    "12. 图复杂测试:",
    (() => {
      const complexGraph = new Graph()
      const vertices = ['A', 'B', 'C', 'D', 'E']
      const edges = [['A', 'B'], ['B', 'C'], ['C', 'D'], ['D', 'E'], ['E', 'A']]
      vertices.forEach(v => complexGraph.addVertex(v))
      edges.forEach(([from, to]) => complexGraph.addEdge(from, to))
      return complexGraph.getVertexCount() === 5 &&
             complexGraph.getEdgeCount() === 5 &&
             complexGraph.hasEdge('A', 'B') === true &&
             complexGraph.hasEdge('E', 'A') === true
    })()
  )
}

testGraphBasic()
